{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Please input your directory for the top level folder\n",
    "folder name : SUBMISSION MODEL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_ = 'INPUT-PROJECT-DIRECTORY/submission_model/' # input only here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### setting other directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_dir = dir_+'2. data/'\n",
    "processed_data_dir = dir_+'2. data/processed/'\n",
    "log_dir = dir_+'4. logs/'\n",
    "model_dir = dir_+'5. models/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "####################################################################################\n",
    "###################### 2-1. nonrecursive model by store ############################\n",
    "####################################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cvs = ['private']\n",
    "STORES = ['CA_1', 'CA_2', 'CA_3', 'CA_4', 'TX_1', 'TX_2', 'TX_3', 'WI_1', 'WI_2', 'WI_3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from  datetime import datetime, timedelta\n",
    "import gc\n",
    "import numpy as np, pandas as pd\n",
    "import lightgbm as lgb\n",
    "\n",
    "import os, sys, gc, time, warnings, pickle, psutil, random\n",
    "\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reduce_mem_usage(df, verbose=False):\n",
    "    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n",
    "    start_mem = df.memory_usage().sum() / 1024**2    \n",
    "    for col in df.columns:\n",
    "        col_type = df[col].dtypes\n",
    "        if col_type in numerics:\n",
    "            c_min = df[col].min()\n",
    "            c_max = df[col].max()\n",
    "            if str(col_type)[:3] == 'int':\n",
    "                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n",
    "                    df[col] = df[col].astype(np.int8)\n",
    "                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n",
    "                       df[col] = df[col].astype(np.int16)\n",
    "                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n",
    "                    df[col] = df[col].astype(np.int32)\n",
    "                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n",
    "                    df[col] = df[col].astype(np.int64)  \n",
    "            else:\n",
    "                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n",
    "                    df[col] = df[col].astype(np.float16)\n",
    "                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n",
    "                    df[col] = df[col].astype(np.float32)\n",
    "                else:\n",
    "                    df[col] = df[col].astype(np.float64)    \n",
    "    end_mem = df.memory_usage().sum() / 1024**2\n",
    "    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "FIRST_DAY = 710\n",
    "remove_feature = ['id',\n",
    "                  'state_id',\n",
    "                  'store_id',\n",
    "#                   'item_id',\n",
    "#                   'dept_id',\n",
    "#                   'cat_id',\n",
    "                  'date','wm_yr_wk','d','sales']\n",
    "\n",
    "cat_var = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + [\"event_name_1\", \"event_name_2\", \"event_type_1\", \"event_type_2\"]\n",
    "cat_var = list(set(cat_var) - set(remove_feature))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid2_colnm = ['sell_price', 'price_max', 'price_min', 'price_std',\n",
    "               'price_mean', 'price_norm', 'price_nunique', 'item_nunique',\n",
    "               'price_momentum', 'price_momentum_m', 'price_momentum_y']\n",
    "\n",
    "grid3_colnm = ['event_name_1', 'event_type_1', 'event_name_2',\n",
    "               'event_type_2', 'snap_CA', 'snap_TX', 'snap_WI', 'tm_d', 'tm_w', 'tm_m',\n",
    "               'tm_y', 'tm_wm', 'tm_dw', 'tm_w_end']\n",
    "\n",
    "lag_colnm = [ 'sales_lag_28', 'sales_lag_29', 'sales_lag_30',\n",
    "             'sales_lag_31', 'sales_lag_32', 'sales_lag_33', 'sales_lag_34',\n",
    "             'sales_lag_35', 'sales_lag_36', 'sales_lag_37', 'sales_lag_38',\n",
    "             'sales_lag_39', 'sales_lag_40', 'sales_lag_41', 'sales_lag_42',\n",
    "             \n",
    "             'rolling_mean_7', 'rolling_std_7', 'rolling_mean_14', 'rolling_std_14',\n",
    "             'rolling_mean_30', 'rolling_std_30', 'rolling_mean_60',\n",
    "             'rolling_std_60', 'rolling_mean_180', 'rolling_std_180']\n",
    "\n",
    "mean_enc_colnm = [\n",
    "    \n",
    "    'enc_store_id_dept_id_mean', 'enc_store_id_dept_id_std', \n",
    "    'enc_item_id_state_id_mean', 'enc_item_id_state_id_std',\n",
    "\n",
    "\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "########################### Make grid\n",
    "#################################################################################\n",
    "def prepare_data(store):\n",
    "    \n",
    "    grid_1 = pd.read_pickle(processed_data_dir+\"grid_part_1.pkl\")\n",
    "    grid_2 = pd.read_pickle(processed_data_dir+\"grid_part_2.pkl\")[grid2_colnm]\n",
    "    grid_3 = pd.read_pickle(processed_data_dir+\"grid_part_3.pkl\")[grid3_colnm]\n",
    "\n",
    "    grid_df = pd.concat([grid_1, grid_2, grid_3], axis=1)\n",
    "    del grid_1, grid_2, grid_3; gc.collect()\n",
    "    \n",
    "    grid_df = grid_df[grid_df['store_id'] == store]\n",
    "    grid_df = grid_df[grid_df['d'] >= FIRST_DAY]\n",
    "    \n",
    "    lag = pd.read_pickle(processed_data_dir+\"lags_df_28.pkl\")[lag_colnm]\n",
    "    \n",
    "    lag = lag[lag.index.isin(grid_df.index)]\n",
    "    \n",
    "    grid_df = pd.concat([grid_df,\n",
    "                     lag],\n",
    "                    axis=1)\n",
    "    \n",
    "    del lag; gc.collect()\n",
    "    \n",
    "\n",
    "    mean_enc = pd.read_pickle(processed_data_dir+\"mean_encoding_df.pkl\")[mean_enc_colnm]\n",
    "    mean_enc = mean_enc[mean_enc.index.isin(grid_df.index)]\n",
    "    \n",
    "    grid_df = pd.concat([grid_df,\n",
    "                         mean_enc],\n",
    "                        axis=1)    \n",
    "    del mean_enc; gc.collect()\n",
    "    \n",
    "    grid_df = reduce_mem_usage(grid_df)\n",
    "    \n",
    "    \n",
    "    \n",
    "    return grid_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "validation = {\n",
    "    'cv1' : [1551, 1610],\n",
    "    'cv2' : [1829,1857],\n",
    "    'cv3' : [1857, 1885],\n",
    "    'cv4' : [1885,1913],\n",
    "    'public' : [1913, 1941],\n",
    "    'private' : [1941, 1969]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cv1 : 2015-04-28 ~ 2015-06-26\n",
    "\n",
    "### cv2 : 2016-02-01 ~ 2016-02-28\n",
    "\n",
    "### cv3 : 2016-02-29 ~ 2016-03-27\n",
    "\n",
    "### cv4 : 2016-03-28 ~ 2016-04-24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "########################### Model params\n",
    "#################################################################################\n",
    "lgb_params = {\n",
    "                    'boosting_type': 'gbdt',\n",
    "                    'objective': 'tweedie',\n",
    "                    'tweedie_variance_power': 1.1,\n",
    "                    'metric': 'rmse',\n",
    "                    'subsample': 0.5,\n",
    "                    'subsample_freq': 1,\n",
    "                    'learning_rate': 0.015,\n",
    "                    'num_leaves': 2**8-1,\n",
    "                    'min_data_in_leaf': 2**8-1,\n",
    "                    'feature_fraction': 0.5,\n",
    "                    'max_bin': 100,\n",
    "                    'n_estimators': 3000,\n",
    "                    'boost_from_average': False,\n",
    "                    'verbose': -1,\n",
    "                    'seed' : 1995\n",
    "                } "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cv : day [1941, 1969]\n",
      "CA_1 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 2.44485\tvalid_1's rmse: 2.79205\n",
      "[200]\tvalid_0's rmse: 3.0841\tvalid_1's rmse: 2.5452\n",
      "[300]\tvalid_0's rmse: 3.24713\tvalid_1's rmse: 2.4777\n",
      "[400]\tvalid_0's rmse: 3.30113\tvalid_1's rmse: 2.43763\n",
      "[500]\tvalid_0's rmse: 3.32392\tvalid_1's rmse: 2.41065\n",
      "[600]\tvalid_0's rmse: 3.3353\tvalid_1's rmse: 2.39203\n",
      "[700]\tvalid_0's rmse: 3.34441\tvalid_1's rmse: 2.3781\n",
      "[800]\tvalid_0's rmse: 3.34779\tvalid_1's rmse: 2.3646\n",
      "[900]\tvalid_0's rmse: 3.34898\tvalid_1's rmse: 2.35449\n",
      "[1000]\tvalid_0's rmse: 3.34508\tvalid_1's rmse: 2.34416\n",
      "[1100]\tvalid_0's rmse: 3.34909\tvalid_1's rmse: 2.33535\n",
      "[1200]\tvalid_0's rmse: 3.34588\tvalid_1's rmse: 2.32786\n",
      "[1300]\tvalid_0's rmse: 3.33983\tvalid_1's rmse: 2.32197\n",
      "[1400]\tvalid_0's rmse: 3.33735\tvalid_1's rmse: 2.31563\n",
      "[1500]\tvalid_0's rmse: 3.33456\tvalid_1's rmse: 2.30922\n",
      "[1600]\tvalid_0's rmse: 3.33134\tvalid_1's rmse: 2.30364\n",
      "[1700]\tvalid_0's rmse: 3.33176\tvalid_1's rmse: 2.29873\n",
      "[1800]\tvalid_0's rmse: 3.3304\tvalid_1's rmse: 2.29393\n",
      "[1900]\tvalid_0's rmse: 3.32861\tvalid_1's rmse: 2.28974\n",
      "[2000]\tvalid_0's rmse: 3.32771\tvalid_1's rmse: 2.28606\n",
      "[2100]\tvalid_0's rmse: 3.32474\tvalid_1's rmse: 2.28232\n",
      "[2200]\tvalid_0's rmse: 3.3239\tvalid_1's rmse: 2.27904\n",
      "[2300]\tvalid_0's rmse: 3.32099\tvalid_1's rmse: 2.27576\n",
      "[2400]\tvalid_0's rmse: 3.31911\tvalid_1's rmse: 2.27194\n",
      "[2500]\tvalid_0's rmse: 3.31603\tvalid_1's rmse: 2.26914\n",
      "[2600]\tvalid_0's rmse: 3.31344\tvalid_1's rmse: 2.26727\n",
      "[2700]\tvalid_0's rmse: 3.31147\tvalid_1's rmse: 2.2643\n",
      "[2800]\tvalid_0's rmse: 3.31247\tvalid_1's rmse: 2.26176\n",
      "[2900]\tvalid_0's rmse: 3.31084\tvalid_1's rmse: 2.25943\n",
      "[3000]\tvalid_0's rmse: 3.3105\tvalid_1's rmse: 2.25758\n",
      "CA_2 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 2.08823\tvalid_1's rmse: 2.13492\n",
      "[200]\tvalid_0's rmse: 2.64895\tvalid_1's rmse: 2.00298\n",
      "[300]\tvalid_0's rmse: 2.86301\tvalid_1's rmse: 1.95886\n",
      "[400]\tvalid_0's rmse: 2.94512\tvalid_1's rmse: 1.93286\n",
      "[500]\tvalid_0's rmse: 2.98867\tvalid_1's rmse: 1.91303\n",
      "[600]\tvalid_0's rmse: 2.9979\tvalid_1's rmse: 1.89895\n",
      "[700]\tvalid_0's rmse: 3.01118\tvalid_1's rmse: 1.88668\n",
      "[800]\tvalid_0's rmse: 3.01143\tvalid_1's rmse: 1.87696\n",
      "[900]\tvalid_0's rmse: 3.01352\tvalid_1's rmse: 1.86808\n",
      "[1000]\tvalid_0's rmse: 3.01326\tvalid_1's rmse: 1.86024\n",
      "[1100]\tvalid_0's rmse: 3.01402\tvalid_1's rmse: 1.85348\n",
      "[1200]\tvalid_0's rmse: 3.01837\tvalid_1's rmse: 1.84756\n",
      "[1300]\tvalid_0's rmse: 3.01901\tvalid_1's rmse: 1.8418\n",
      "[1400]\tvalid_0's rmse: 3.01872\tvalid_1's rmse: 1.8364\n",
      "[1500]\tvalid_0's rmse: 3.02073\tvalid_1's rmse: 1.83191\n",
      "[1600]\tvalid_0's rmse: 3.02029\tvalid_1's rmse: 1.82741\n",
      "[1700]\tvalid_0's rmse: 3.02263\tvalid_1's rmse: 1.82309\n",
      "[1800]\tvalid_0's rmse: 3.02571\tvalid_1's rmse: 1.81905\n",
      "[1900]\tvalid_0's rmse: 3.02265\tvalid_1's rmse: 1.81536\n",
      "[2000]\tvalid_0's rmse: 3.02586\tvalid_1's rmse: 1.81199\n",
      "[2100]\tvalid_0's rmse: 3.02603\tvalid_1's rmse: 1.80874\n",
      "[2200]\tvalid_0's rmse: 3.0302\tvalid_1's rmse: 1.80575\n",
      "[2300]\tvalid_0's rmse: 3.02819\tvalid_1's rmse: 1.80283\n",
      "[2400]\tvalid_0's rmse: 3.02673\tvalid_1's rmse: 1.80024\n",
      "[2500]\tvalid_0's rmse: 3.02511\tvalid_1's rmse: 1.79738\n",
      "[2600]\tvalid_0's rmse: 3.02533\tvalid_1's rmse: 1.79494\n",
      "[2700]\tvalid_0's rmse: 3.02346\tvalid_1's rmse: 1.79236\n",
      "[2800]\tvalid_0's rmse: 3.02334\tvalid_1's rmse: 1.79015\n",
      "[2900]\tvalid_0's rmse: 3.0241\tvalid_1's rmse: 1.78802\n",
      "[3000]\tvalid_0's rmse: 3.02882\tvalid_1's rmse: 1.78569\n",
      "CA_3 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 3.6018\tvalid_1's rmse: 3.9937\n",
      "[200]\tvalid_0's rmse: 4.67396\tvalid_1's rmse: 3.52503\n",
      "[300]\tvalid_0's rmse: 4.96187\tvalid_1's rmse: 3.40879\n",
      "[400]\tvalid_0's rmse: 5.03428\tvalid_1's rmse: 3.34132\n",
      "[500]\tvalid_0's rmse: 5.03474\tvalid_1's rmse: 3.29656\n",
      "[600]\tvalid_0's rmse: 5.05003\tvalid_1's rmse: 3.27214\n",
      "[700]\tvalid_0's rmse: 5.04229\tvalid_1's rmse: 3.25166\n",
      "[800]\tvalid_0's rmse: 5.04212\tvalid_1's rmse: 3.23552\n",
      "[900]\tvalid_0's rmse: 5.04294\tvalid_1's rmse: 3.21925\n",
      "[1000]\tvalid_0's rmse: 5.03757\tvalid_1's rmse: 3.20518\n",
      "[1100]\tvalid_0's rmse: 5.03312\tvalid_1's rmse: 3.19311\n",
      "[1200]\tvalid_0's rmse: 5.02757\tvalid_1's rmse: 3.18144\n",
      "[1300]\tvalid_0's rmse: 5.03141\tvalid_1's rmse: 3.17259\n",
      "[1400]\tvalid_0's rmse: 5.02816\tvalid_1's rmse: 3.16354\n",
      "[1500]\tvalid_0's rmse: 5.02309\tvalid_1's rmse: 3.15803\n",
      "[1600]\tvalid_0's rmse: 5.0223\tvalid_1's rmse: 3.15072\n",
      "[1700]\tvalid_0's rmse: 5.0185\tvalid_1's rmse: 3.14579\n",
      "[1800]\tvalid_0's rmse: 5.02286\tvalid_1's rmse: 3.13993\n",
      "[1900]\tvalid_0's rmse: 5.01836\tvalid_1's rmse: 3.13453\n",
      "[2000]\tvalid_0's rmse: 5.01021\tvalid_1's rmse: 3.12903\n",
      "[2100]\tvalid_0's rmse: 5.00962\tvalid_1's rmse: 3.12389\n",
      "[2200]\tvalid_0's rmse: 5.00592\tvalid_1's rmse: 3.12021\n",
      "[2300]\tvalid_0's rmse: 5.01184\tvalid_1's rmse: 3.1156\n",
      "[2400]\tvalid_0's rmse: 5.00852\tvalid_1's rmse: 3.11081\n",
      "[2500]\tvalid_0's rmse: 5.00771\tvalid_1's rmse: 3.10609\n",
      "[2600]\tvalid_0's rmse: 5.00929\tvalid_1's rmse: 3.10291\n",
      "[2700]\tvalid_0's rmse: 5.0109\tvalid_1's rmse: 3.09895\n",
      "[2800]\tvalid_0's rmse: 5.00641\tvalid_1's rmse: 3.09574\n",
      "[2900]\tvalid_0's rmse: 5.0038\tvalid_1's rmse: 3.09295\n",
      "[3000]\tvalid_0's rmse: 5.00186\tvalid_1's rmse: 3.0896\n",
      "CA_4 start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.31449\tvalid_1's rmse: 1.58263\n",
      "[200]\tvalid_0's rmse: 1.52336\tvalid_1's rmse: 1.5128\n",
      "[300]\tvalid_0's rmse: 1.58723\tvalid_1's rmse: 1.49281\n",
      "[400]\tvalid_0's rmse: 1.61557\tvalid_1's rmse: 1.47954\n",
      "[500]\tvalid_0's rmse: 1.63436\tvalid_1's rmse: 1.46961\n",
      "[600]\tvalid_0's rmse: 1.64479\tvalid_1's rmse: 1.4612\n",
      "[700]\tvalid_0's rmse: 1.6476\tvalid_1's rmse: 1.454\n",
      "[800]\tvalid_0's rmse: 1.65455\tvalid_1's rmse: 1.44774\n",
      "[900]\tvalid_0's rmse: 1.65474\tvalid_1's rmse: 1.4423\n",
      "[1000]\tvalid_0's rmse: 1.65371\tvalid_1's rmse: 1.43745\n",
      "[1100]\tvalid_0's rmse: 1.6537\tvalid_1's rmse: 1.4328\n",
      "[1200]\tvalid_0's rmse: 1.65528\tvalid_1's rmse: 1.42865\n",
      "[1300]\tvalid_0's rmse: 1.65635\tvalid_1's rmse: 1.42503\n",
      "[1400]\tvalid_0's rmse: 1.65841\tvalid_1's rmse: 1.42183\n",
      "[1500]\tvalid_0's rmse: 1.65421\tvalid_1's rmse: 1.41874\n",
      "[1600]\tvalid_0's rmse: 1.65636\tvalid_1's rmse: 1.41596\n",
      "[1700]\tvalid_0's rmse: 1.65552\tvalid_1's rmse: 1.41335\n",
      "[1800]\tvalid_0's rmse: 1.65521\tvalid_1's rmse: 1.41101\n",
      "[1900]\tvalid_0's rmse: 1.65608\tvalid_1's rmse: 1.40851\n",
      "[2000]\tvalid_0's rmse: 1.6559\tvalid_1's rmse: 1.40633\n",
      "[2100]\tvalid_0's rmse: 1.65392\tvalid_1's rmse: 1.40429\n",
      "[2200]\tvalid_0's rmse: 1.65366\tvalid_1's rmse: 1.40229\n",
      "[2300]\tvalid_0's rmse: 1.65327\tvalid_1's rmse: 1.40031\n",
      "[2400]\tvalid_0's rmse: 1.65363\tvalid_1's rmse: 1.39843\n",
      "[2500]\tvalid_0's rmse: 1.65208\tvalid_1's rmse: 1.3966\n",
      "[2600]\tvalid_0's rmse: 1.65171\tvalid_1's rmse: 1.39502\n",
      "[2700]\tvalid_0's rmse: 1.6515\tvalid_1's rmse: 1.39339\n",
      "[2800]\tvalid_0's rmse: 1.64951\tvalid_1's rmse: 1.39189\n",
      "[2900]\tvalid_0's rmse: 1.64997\tvalid_1's rmse: 1.3905\n",
      "[3000]\tvalid_0's rmse: 1.64805\tvalid_1's rmse: 1.3892\n",
      "[1300]\tvalid_0's rmse: 2.76374\tvalid_1's rmse: 1.92064\n",
      "[1400]\tvalid_0's rmse: 2.77373\tvalid_1's rmse: 1.91503\n",
      "[1500]\tvalid_0's rmse: 2.77287\tvalid_1's rmse: 1.91021\n",
      "[1600]\tvalid_0's rmse: 2.77843\tvalid_1's rmse: 1.90494\n",
      "[1700]\tvalid_0's rmse: 2.77568\tvalid_1's rmse: 1.90091\n",
      "[1800]\tvalid_0's rmse: 2.77378\tvalid_1's rmse: 1.89695\n",
      "[1900]\tvalid_0's rmse: 2.77614\tvalid_1's rmse: 1.89363\n",
      "[2000]\tvalid_0's rmse: 2.77241\tvalid_1's rmse: 1.89062\n",
      "[2100]\tvalid_0's rmse: 2.77239\tvalid_1's rmse: 1.88717\n",
      "[2200]\tvalid_0's rmse: 2.76853\tvalid_1's rmse: 1.88483\n",
      "[2300]\tvalid_0's rmse: 2.76859\tvalid_1's rmse: 1.88213\n",
      "[2400]\tvalid_0's rmse: 2.77441\tvalid_1's rmse: 1.87913\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2700]\tvalid_0's rmse: 2.76787\tvalid_1's rmse: 1.87233\n",
      "[2800]\tvalid_0's rmse: 2.76412\tvalid_1's rmse: 1.87044\n",
      "[2900]\tvalid_0's rmse: 2.76043\tvalid_1's rmse: 1.86863\n",
      "[3000]\tvalid_0's rmse: 2.76101\tvalid_1's rmse: 1.8671\n",
      "TX_2 start\n"
     ]
    }
   ],
   "source": [
    "########################### Train Models\n",
    "#################################################################################\n",
    "from lightgbm import LGBMRegressor\n",
    "from gluonts.model.rotbaum._model import QRX\n",
    "rmsse_bycv = dict()\n",
    "\n",
    "for cv in cvs:\n",
    "    print('cv : day', validation[cv])\n",
    "\n",
    "    pred_list = []\n",
    "    for store in STORES:\n",
    "\n",
    "        print(store, 'start')\n",
    "        grid_df = prepare_data(store)\n",
    "\n",
    "        model_var = grid_df.columns[~grid_df.columns.isin(remove_feature)]\n",
    "\n",
    "        tr_mask = (grid_df['d'] <= validation[cv][0]) & (grid_df['d'] >= FIRST_DAY)\n",
    "        vl_mask = (grid_df['d'] > validation[cv][0]) & (grid_df['d'] <= validation[cv][1])\n",
    "\n",
    "        estimator = QRX(model=LGBMRegressor(**lgb_params),\n",
    "                    min_bin_size=200)\n",
    "        estimator.fit(\n",
    "            grid_df[tr_mask][model_var], \n",
    "            grid_df[tr_mask]['sales'],\n",
    "            max_sample_size=1000000, \n",
    "            seed=1,\n",
    "            eval_set=[(\n",
    "                    grid_df[vl_mask][model_var],\n",
    "                    grid_df[vl_mask]['sales']\n",
    "                ),\n",
    "                (\n",
    "                    grid_df[tr_mask][model_var], \n",
    "                    grid_df[tr_mask]['sales']\n",
    "                )\n",
    "            ],\n",
    "            verbose=100,\n",
    "            x_train_is_dataframe=True\n",
    "        )\n",
    "\n",
    "#        display(pd.DataFrame({'name':m_lgb.feature_name(),\n",
    "#                              'imp':m_lgb.feature_importance()}).sort_values('imp',ascending=False).head(25))\n",
    "        \n",
    "        model_name = model_dir+'non_recur_model_'+store+'.bin'\n",
    "        pickle.dump(estimator, open(model_name, 'wb'))\n",
    "        \n",
    "        del grid_df, estimator, tr_mask, vl_mask; gc.collect() #train_data, valid_data,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "conda_mxnet_p36",
   "language": "python",
   "name": "conda_mxnet_p36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
